﻿using System;
using System.Linq;

namespace YK.Infrastructure.NH
{
    using YK.Component.Tools;
    using YK.Component.Data;


    public class RepositoryBase<T> : IRepository<T> where T : EntityBase
    {
        private Database database;

        protected RepositoryBase(IDatabaseFactory databasFactory)
        {
            DatabaseFactory = databasFactory;
        }

        protected IDatabaseFactory DatabaseFactory
        {
            get;
            private set;
        }

        protected Database Database
        {
            get
            {
                return database ?? (database = DatabaseFactory.Get());
            }
        }

        #region interface
        public void Add(T entity)
        {
            Database.Add<T>(entity);
        }

        public void Update(T entity)
        {
            Database.Update<T>(entity);
        }

        public void Delete(T entity)
        {
            Database.Delete<T>(entity);
        }

        public void Delete(System.Linq.Expressions.Expression<Func<T, bool>> where)
        {

            throw new NotImplementedException();
        }

        public T GetById(long Id)
        {
            throw new NotImplementedException();
        }

        public T GetById(string Id)
        {
            return Database.GetById<T>(Id);
        }

        public T Get(System.Linq.Expressions.Expression<Func<T, bool>> where)
        {
            //Database.CreateQuery<T>().Select(where).FirstOrDefault();
            throw new NotImplementedException();
        }

        public IQueryable<T> GetAll()
        {
            return Database.CreateQuery<T>();
        }

        public IQueryable<T> GetMany(System.Linq.Expressions.Expression<Func<T, bool>> where)
        {
            throw new NotImplementedException();
        }
        #endregion

    }
}
